<?php
namespace app\flash\controller;
use QL\QueryList;
use think\Db;
use think\Controller;
use think\Exception;
use think\Common;
use think\Request;
use think\Validate;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
//header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http动作
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
    exit;
}
class Comment extends Controller
{
    //一级评论
    public function  first_comments(){
        $data = input('');//
        $validate = Validate::make([
            'id'  => 'require',
            'flag'=>'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $id = $data['id'];
        //评论页码
        $result = db('comment')
            ->alias('fc')
            ->join('user u','u.id = fc.from_id','left')
            ->where(array('fc.message_id'=>$id,'fc.flag'=>$data['flag']))
            ->field('fc.id,fc.from_id,fc.message_id,fc.content,u.username,u.phone,like_count,u.picture,fc.create_time,fc.reply_num')
            ->paginate(20,false,['query'=>request()->param()])->each(function ($item,$key) use($data){
                $item['content'] = json_decode($item['content'],true);
                if (empty($item['username'])) {
                    $item['username'] = $item['phone'];
                }  
                unset($item['phone']);            
                if(!empty($data['user_id'])){
                    $user_id = $data['user_id'];
                    $zan = Db::table('like_all')->where(array('user_id'=>$user_id,'comment_id'=>$item['id']))->find();
                    if(count($zan)>0){
                        $item['is_zan'] = 1;
                    }else{
                        $item['is_zan'] = 0;
                    }
                }else{
                    $item['is_zan'] = 0;
                }
                return $item;
            });
        return json(msg(1,$result,'一级评论查询成功'));
    }

    //子孙评论
    public function zisun_comments(){
        $data = input('');//
        $validate = Validate::make([
            'id'  => 'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $id = $data['id'];
        $detail = Db::table('comment')
            ->alias('c')
            ->join('user u','u.id = c.from_id','left')
            ->field('c.id,c.from_id,c.message_id,c.content,u.username,u.phone,like_count,u.picture,c.create_time')
            ->where(array('c.id'=>$id))
            ->find();
        $detail['content'] =  json_decode($detail['content'],true);
        $user_id = input('user_id');
        if(!empty($user_id)){
            $zan = Db::table('like_all')->where(array('user_id'=>$user_id,'comment_id'=>$detail['id']))->find();
            if(count($zan)>0){
                $detail['is_zan'] = 1;
            }else{
                $detail['is_zan'] = 0;
            }
        }
        $zisun = Db::table('reply')
            ->alias('r')
            ->join('user u','u.id = r.from_id','left')
            ->join('user p','p.id = r.to_id','left')
            ->join('reply pr','r.parent_id = pr.id','left')
            ->field('r.id,r.from_id,r.content,u.username,u.phone,r.like_count,u.picture,r.create_time,r.parent_id,r.to_id,p.username as p_username,p.phone as p_phone,p.picture as p_picture,pr.content as p_content')
            ->where(array('r.cid'=>$id))
            ->paginate(20,false,['query'=>request()->param()])->each(function ($item,$key){
                $item['content'] = json_decode($item['content'],true);
                if (empty($item['username'])) {
                    $item['username'] = $item['phone'];
                }
                unset($item['phone']);
                if($item['parent_id']==0){
                    $item["p_username"] = null;
                    $item["p_phone"] = null;
                    $item["p_picture"] = null;
                    $item["p_content"] = null;
                }else{
                    if (empty($item['p_username'])) {
                        $item['p_username'] = $item['p_phone'];
                    }
                    unset($item['p_phone']);
                    $item['p_content'] = json_decode($item['p_content'],true);
                }
                $user_id = input('user_id');
                if(!empty($user_id)){
                    $zan = Db::table('like_all')->where(array('user_id'=>$user_id,'comment_id'=>$item['id']))->find();
                    if(count($zan)>0){
                        $item['is_zan'] = 1;
                    }else{
                        $item['is_zan'] = 0;
                    }
                }
                return $item;
            });
        return json(msg(1,compact('detail','zisun'),'子孙评论查询成功'));
    }

    public function like(){
        $id         = input('id');//评论ID
        $like_count = input('like_count');//评论状态 1-点赞 2-取消点赞
        $type = input('type');//1:评论2.回复
        $user_id    = input('user_id');//用户id
        $create_time= strtotime(date('Y-m-d H:i:s'));//创建时间
        if (empty($id)) {
            return json(msg(0,'','id未找到!'));
        }
        if($type=1){
            $tableName = 'comment';
        }else{
            $tableName = 'reply';
        }
        if ($like_count==1) {
            if ($user_id) {
                //查询是否点赞
                $select = Db::table('like_all')
                    ->where(array( 'user_id'   => $user_id,
                    'comment_id'=> $id,
                        'type'=>$type))
                    ->find();
                if($select){
                    return json(msg(0,'','您已点赞，不可重复点'));
                }
                $arr = array(
                    'user_id'   => $user_id,
                    'comment_id'=> $id,
                    'create_time' => $create_time,
                    'type'=>$type,
                );
                //查询被点赞人id
                $arr['to_id'] = Db::table($tableName)->where('id',$id)->value('from_id');
                if($type=1){
                    $comment = Db::table('comment')
                        ->field('message_id,flag')
                        ->where(array('id'=>$id))
                        ->find();
                    $arr['message_id'] = $comment['message_id'];
                    $arr['flag'] = $comment['flag'];
                }else{
                    $reply = Db::table('reply')
                        ->alias('r')
                        ->join('comment c','r.cid=c.id')
                        ->field('c.message_id,c.flag')
                        ->where(array('id'=>$id))
                        ->find();
                    $arr['message_id'] = $reply['message_id'];
                    $arr['flag'] = $reply['flag'];
                }
                $result = Db::table('like_all')->insert($arr);
                if (!$result) {
                    return json(msg(0,'','点赞失败'));
                }
            }
            $res = Db::table($tableName)->where(['id'=>$id])->setInc('like_count');
        }else{
            $find = Db::table($tableName)->where(['id'=>$id])->find();
            if ($find['like_count']==0) {
                return json(msg(0,'','不能再点了！'));
            }
            $res = Db::table($tableName)->where(['id'=>$id])->setDec('like_count');
            if ($user_id) {
                $find = Db::table('like_all')
                    ->where(array( 'user_id'=> $user_id,
                    'comment_id'=> $id))->find();
                if(!$find){
                    return json(msg(0,'','未找到您的点赞'));
                }
                $res = Db::table('like_all')->where(['comment_id'=>$id,'user_id'=>$user_id])->delete();
            }
        }
        if ($res) {
            return json(msg(1,'','成功'));
        }else{
            return json(msg(0,'','失败'));
        }
    }


}
